'use strict';

/**
 * @description 回复表模型
 * */
module.exports = app => {
  const { TEXT, INTEGER } = app.Sequelize;

  const Reply = app.model.define('reply', {
    content: {
      type: TEXT,
      allowNull: false,
      comment: '评论内容',
    },
    houseId: {
      type: INTEGER,
      allowNull: true,
      comment: '房屋id',
    },
    cid: {
      type: INTEGER,
      allowNull: true,
      comment: '论坛id',
    },
    uid: {
      type: INTEGER,
      allowNull: false,
      comment: '回复人id',
    },
    rid: {
      type: INTEGER,
      allowNull: true,
      comment: '回复id',
    },
    status: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: '审核状态 1-待审核 2-同意 3-拒绝',
    },
  });

  Reply.associate = () => {
    app.model.Reply.belongsTo(app.model.User, {
      foreignKey: 'uid',
    });
    app.model.Reply.belongsTo(app.model.Comment, {
      foreignKey: 'cid',
    });
    app.model.Reply.belongsTo(app.model.House, {
      foreignKey: 'houseId',
    });
    app.model.Reply.hasMany(app.model.Reply, {
      foreignKey: 'rid',
      as: 'ToReply',
    });
    app.model.Reply.belongsTo(app.model.Reply, {
      foreignKey: 'rid',
      as: 'BeReply',
    });
  };

  return Reply;
};
